<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Category_Model extends MY_Model {
    public function __construct()
    {
        parent::__construct();
        $this->load->model('entity/category_entity', "Category_Entity");
    }

    public function getCategories($options=array())
    {
        $result = null;
        $this->db->select('id,title,slug,order,icon,deleted_at', true);
        $this->db->from($this->table_category_name);
        if(count($options) > 0){
            $this->db->where($options);
        } else {
            $this->db->where(array('deleted_at' => null));
        }
        $this->db->order_by('id', 'asc');
        $this->db->order_by('order', 'asc');
        $result = $this->db->get()->result('Category_Entity');
        return $result;
    }

    public function getShopCategoryBySlug($slug, $shop_id)
    {
        $select = "
            a.id, a.title, a.slug, a.cate_id, a.shop_id, a.order, a.deleted_at,
            d.title AS cate_title, d.slug AS cate_slug";
        $this->db->select($select, true);
        $this->db->from($this->table_shop_category_name . ' AS a');
        $this->db->join($this->table_shop_name . ' b', 'a.shop_id=b.id');
        $this->db->join($this->table_user_name . ' c', 'b.user_id=c.id');
        $this->db->join($this->table_category_name . ' d', 'a.cate_id=d.id');
        $this->db->where('a.shop_id', $shop_id);
        $this->db->where('b.deleted_at', null);
        $this->db->where('c.deleted_at', null);
        $this->db->where('d.deleted_at', null);
        $this->db->where('a.slug', $slug);
        $this->load->model('entity/shop_category_entity', 'Shop_Category_Entity');
        return $this->db->get()->first_row('Shop_Category_Entity');
    }

    public function getCateBySlug($slug, $getAll=false)
    {
        $this->db->select('id, title, slug, order,icon, deleted_at', true);
        $this->db->from($this->table_category_name);
        $this->db->where('slug', $slug);
        if(!$getAll){
            $this->db->where('deleted_at', null);
        }
        $result = $this->db->get()->result('Category_Entity');
        if ($result!=null && count($result) > 0) {
            return $result[0];
        }
        return null;
    }

    public function checkCategoryExists($slug, $id=null){
        $select = "a.id";
        $this->db->select($select, true);
        $this->db->from($this->table_category_name . ' AS a');
        $this->db->where('a.slug', $slug);
        if($id!=null){
            $this->db->where('a.id <>', $id);
        }
        return $this->db->count_all_results();
    }

    /**
     * For admin only
     * Add new category
     */
    public function insert($category)
    {
        $this->db->trans_begin();
        $this->db->insert($this->table_category_name, $category);
        if ($this->db->trans_status() === false) {
            $this->db->trans_rollback();
            return false;
        } else {
            $this->db->trans_commit();
        }
        return true;
    }

    /**
     * For admin only
     * update a category
     */
    public function update($category_id, $category)
    {
        $this->db->trans_begin();
        $this->db->where('id', $category_id);
        $this->db->update($this->table_category_name, $category);
        if ($this->db->trans_status() === false) {
            $this->db->trans_rollback();
            return false;
        } else {
            $this->db->trans_commit();
        }
        return true;
    }
}
?>